<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="showModel(false, 'Add')">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      show-footer
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
      :loading="loading"
    >
      <vxe-table-column>
        <vxe-table-column field="licenseNumber" title="车牌号" min-width="130" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.licenseNumber" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="trailerNum" title="挂车号" min-width="130" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.trailerNum" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="strongInsurance" title="交强险" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.strongInsuranceStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.strongInsuranceEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="vehicleLossInsurance" title="车辆损失险" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.vehicleLossInsuranceStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.vehicleLossInsuranceEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="thirdPartyLiabilityInsurance"
          title="第三者责任险"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input
                v-model="listQuery.thirdPartyLiabilityInsuranceStart"
                placeholder="下限"
                clearable
              />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input
                v-model="listQuery.thirdPartyLiabilityInsuranceEnd"
                placeholder="上限"
                clearable
              />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="personnelInsurance" title="人员险" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.personnelInsuranceStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.personnelInsuranceEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="nonDeductibleInsurance" title="不计免赔特约险" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.nonDeductibleInsuranceStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.nonDeductibleInsuranceEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="insuranceAmount" title="合计金额" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.insuranceAmountStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.insuranceAmountEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="vehicleTax" title="车船税" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.vehicleTaxStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.vehicleTaxEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="liabilityInsurance" title="责任险" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.liabilityInsuranceStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.liabilityInsuranceEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="commercialCompany" title="商业险保险公司" min-width="300" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.commercialCompany" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="compulsoryCompany" title="交强险投保公司" min-width="300" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.compulsoryCompany" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="liabilityCompany" title="责任险投保公司" min-width="300" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.liabilityCompany" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="liabilityDueDate" title="责任险到期日期" min-width="420" :resizable="false" sortable />
        <template v-slot:header>
          <el-date-picker
            v-model="liabilityDueDateRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'liabilityDueDate')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="compulsoryDueDate" title="交强险到期日期" min-width="420" :resizable="false" sortable />
        <template v-slot:header>
          <el-date-picker
            v-model="compulsoryDueDateRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'compulsoryDueDate')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="commercialDueDate" title="商业险到期日期" min-width="420" :resizable="false" sortable />
        <template v-slot:header>
          <el-date-picker
            v-model="commercialDueDateRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'commercialDueDate')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="gmtCreated" title="创建时间" min-width="420" :resizable="false" sortable />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right" min-width="100">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="code => showModel(scope.row.code, 'Edit')">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="600" height="800" resize>
      <compAdd ref="AddRef" :restaurants="restaurants" @hideModel="hideModel" />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="600" height="800" resize>
      <compEdit ref="EditRef" :restaurants="restaurants" @hideModel="hideModel" />
    </vxe-modal>
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ["cookie"] });
import compAdd from "./components/vehicleInsuranceAdd.vue";
import compEdit from "./components/vehicleInsuranceEdit.vue";
import { vehicleInsurance, delVehicleInsurance } from "@/api/vehicleService";
import { vehicle } from "@/api/transport";
export default {
  name: "Parent",
  components: {
    compAdd,
    compEdit
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      loading: false,
      tableData: [],
      restaurants: [],
      liabilityDueDateRange: "",
      compulsoryDueDateRange: "",
      commercialDueDateRange: "",
      gmtCreatedRange: "",
      modelAddFlag: false,
      modelEditFlag: false,
      partyType: this.$store.getters.party.partyType,
      partyCode: this.$store.getters.party.code,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        sortField: "",
        sortType: "",
        total: 0
      }
    };
  },
  created() {
    if(this.partyType == 'DRIVER') {
      vehicle({ pageSize: 1000000 , partyCode: this.partyCode}).then(response => {
        this.restaurants = response.data.records;
      });
    }else if(this.partyType == 'DRIVER_LEADER') {
      vehicle({ pageSize: 1000000 , teamCode: this.partyCode}).then(response => {
        this.restaurants = response.data.records;
      });
    }else {
      vehicle({ pageSize: 1000000 }).then(response => {
        this.restaurants = response.data.records;
      });
    }
    this.findList();
  },





  methods: {
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        vehicleInsurance(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.loading = false;
          resolve();
        });
      })
    },
    // 时间
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 展示弹框
    showModel(code, prefix) {
      if (code) {
        this.$refs[`${prefix}Ref`].getInfo(code);
      }
      this[`model${prefix}Flag`] = true;
    },
    // 隐藏弹框
    hideModel() {
      this.modelAddFlag = false;
      this.modelEditFlag = false;
      this.findList();
    },
    // 删除
    funcDelete(codes) {
      this.$confirm("确认删除吗？").then(_ => {
        delVehicleInsurance({ codes }).then(response => {
          this.$message({ message: response.msg, type: "success" });
          this.findList();
        });
      });
    },

    // 分页
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    },
    // 设置底部合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "合计";
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.sum(data, column.property);
          }
          return null;
        })
      ];
    }
  }
};
</script>
